import {defineConfig, loadEnv} from 'vite'
import react from '@vitejs/plugin-react'
import {viteMockServe} from 'vite-plugin-mock'

const path = require('path')

const host = `http://127.0.0.1:9999`;
const oauthHost = `http://127.0.0.1:9081`;

const localEnabled: boolean = (process.env.USE_MOCK as unknown as boolean) || false;
const prodEnabled: boolean = (process.env.VITE_USE_CHUNK_MOCK as unknown as boolean) || false;

// https://vitejs.dev/config/
export default defineConfig({
  base: '/cloud-front/',
  plugins: [
    react(),
    viteMockServe({
      mockPath: 'mock',
      logger: true, //是否在控制台显示请求日志
      supportTs: true, // 打开后，可以读取 ts 文件模块。 请注意，打开后将无法监视.js 文件。
      watchFiles: true, // 监视文件更改
      localEnabled: localEnabled,
      prodEnabled: prodEnabled,
    }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  server: {
    cors: true,
    open: true,//服务启动时自动在浏览器中打开应用
    port: 3001,
    strictPort: false, //设为true时端口被占用则直接退出，不会尝试下一个可用端口
    proxy: {
      '/gateway/': {
        target: host,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/gateway/, '')
      },
      '/api/': {
        target: oauthHost,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/auth/': {
        target: oauthHost,
        changeOrigin: true
      },
      '/v1/app/': {
        target: `http://127.0.0.1:8090`,
        changeOrigin: true,
        rewrite: (path) => path
      }
    }
  }
})
